package com.hllxd.hism.star.basic.service.dao.influx;


import com.hllxd.hism.star.basic.service.config.influxdb.InfluxDBConnect;
import com.hllxd.hism.star.basic.service.model.dapan.SeriesResult;
import lombok.extern.slf4j.Slf4j;
import org.influxdb.dto.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * @author xiyong.lxy
 * @date 2018-12-21 8:53
 */
@Slf4j
@Service("dapanInfluxMapper")
public class DapanInfluxMapperImpl implements DapanInfluxMapper {


    @Autowired
    private InfluxDBConnect influxDBConnect;

    private List<String> hosts = new ArrayList<>();

    @PostConstruct
    private void init(){
        hosts.add("hbase-slave05.sm.bj.com");
        hosts.add("hbase-slave07.sm.bj.com");
        hosts.add("host-10-10-20-12");
        hosts.add("host-10-10-20-14");
    }

    /** 中间件监控日志 */
    public static final String TSAR_INFLUX_DB = "bcp_tsar";

    @Override public SeriesResult listSeries(String dt, String dt2, String tag, String tableName,String database,String dtType) {

        if(TSAR_INFLUX_DB.equals(database)){
            // 中间件指标
           return queryTsarSeries(dt, dt2, tag, tableName, database, dtType);
        }else{
            // 业务指标 dtType=1m
            return queryBcpSeries(dt, dt2, tag, tableName, database, dtType);
        }
    }
    @Override public SeriesResult listBcpSeries(String dt, String dt2, String tag, String table) {
        // 业务指标
        return queryBcpSeries(dt, dt2, tag, table, "bcp_time_series", "1m");
    }

    @Override public SeriesResult listRtSeries(String dt, String dt2, String tag, String table, String dimcombo) {
        // 业务指标
        return queryRtSeries(dt, dt2, tag, table, dimcombo, "bcp_time_series");
    }

    private SeriesResult queryTsarSeries(String dt, String dt2, String tag, String tableName,String database,String dtType) {
        SeriesResult result;
        List<SeriesResult> seriesResultList = new ArrayList<>();
        for(String host:hosts) {
            String command = String.format(""
                            + "SELECT sum(\"value\") as value "
                            + "FROM %s "
                            + "WHERE mod='%s' and time >= '%s' and time <= '%s' and \"host\"='%s'"
                            + "GROUP BY time(1m);",
                    tableName, tag, dt, dt2, host);
            log.info("listSeries command:{}",command);
            List<QueryResult.Result> results = influxDBConnect.query(command,database);
            seriesResultList.add(SeriesResult.buildEntryList(results,host));
        }
        result = SeriesResult.buildList(seriesResultList);
        log.info("listSeries result:{}",seriesResultList.size());
        return result;
    }

    private SeriesResult queryBcpSeries(String dt, String dt2, String tag, String tableName,String database,String dtType) {
        String command = String.format(""
                        + "SELECT sum(\"%s\") as value "
                        + "FROM \"%s\" "
                        + "WHERE time >= '%s' and time <= '%s' "
                        + "GROUP BY time(%s);",
                tag,tableName,dt,dt2,dtType);
        log.info("listSeries command:{}",command);
        List<QueryResult.Result> results = influxDBConnect.query(command,database);
        if(results==null) {
            return null;
        }
        return SeriesResult.buildEntryList(results,"");
    }

    /**
     * 查询是数据
     * @param dt 开始
     * @param dt2 结束
     * @param tag 标签
     * @param tableName 表名
     * @param dimCombo 空的时候为true
     * @param database 数据库名
     * @return 序列
     */
    private SeriesResult queryRtSeries(String dt, String dt2, String tag, String tableName,String dimCombo,String database) {
        String command = String.format(""
                        + "SELECT sum(\"%s\") as value "
                        + "FROM \"%s\" "
                        + "WHERE (%s) and time >= '%s' and time <= '%s' "
                        + "GROUP BY time(5m);",
                tag,tableName,dimCombo,dt,dt2);
        log.info("listSeries command:{}",command);
        List<QueryResult.Result> results = influxDBConnect.query(command,database);
        if(results==null) {
            return null;
        }
        return SeriesResult.buildEntryList(results,"");
    }

}
